﻿Imports System.IO.Ports
Imports System.Text
Imports System.Text.RegularExpressions


Public Class Form1
    Dim WithEvents myengine As New engine
    Dim settingscontrol As New Settings
    Dim RecieveLogControl As New recieve
    Dim mailforwarder As New mailforward

    Dim ReadMessageThread As New ArrayList

    Sub LoadSettings()
        With settingscontrol
            Dim ports As String() = SerialPort.GetPortNames
            For Each item In ports
                .cmbPorts.Items.Add(item)
            Next
            .cmbPorts.SelectedItem = My.Settings.Portname
            .cmbbits.SelectedItem = My.Settings.BitsPerSecond
            .cmbdata.SelectedItem = My.Settings.Databits
            .cmbparity.SelectedItem = My.Settings.Parity
            .cmbstopbits.SelectedItem = My.Settings.StopBits
            .cmbflow.SelectedItem = My.Settings.FlowControl
        End With
        Timer1.Interval = Integer.Parse(My.Settings.GetSMSHeartBeat.ToString)
        myengine.Append("Settings Loaded")
        If System.IO.File.Exists(My.Settings.maillistloc) = True Then
            Dim objReader As New System.IO.StreamReader(My.Settings.maillistloc)
            Do While objReader.Peek() <> -1
                Dim spliter As String()
                spliter = objReader.ReadLine().Split("|")
                myengine.Keylist.Add(spliter(0))
                myengine.MailValue.Add(spliter(1))
            Loop
        End If
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        myengine.Append("Session started.")
        LoadSettings()
        myengine.SerialPort1 = SerialPort1
        myengine.InsertAllMessages()
        'Timer1.Enabled = True
        TreeView1.ExpandAll()

        RecieveLogControl.MainForm = Me
    End Sub
    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        SplitContainer1.Panel2.Controls.Clear()
        If TreeView1.SelectedNode.Text = "Settings" Then
            SplitContainer1.Panel2.Controls.Add(settingscontrol)
        ElseIf TreeView1.SelectedNode.Text = "Recieve Log" Then
            SplitContainer1.Panel2.Controls.Add(RecieveLogControl)
        ElseIf TreeView1.SelectedNode.Text = "Email Forwarding" Then
            SplitContainer1.Panel2.Controls.Add(mailforwarder)
        ElseIf TreeView1.SelectedNode.Text = "Event Log" Then
            Dim EventlogControl As New eventlogs
            SplitContainer1.Panel2.Controls.Add(EventlogControl)
        End If

    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If ReadMessageThread.Count = 0 Then
            'Logs
            If myengine.DeviceGood = False Then
                Timer1.Enabled = False
            End If
            Dim t As New Threading.Thread(AddressOf myengine.InsertUnreadMessages)
            t.Start()
            ReadMessageThread.Add(t)
        Else
            For Each item As Threading.Thread In ReadMessageThread
                item.Abort()
            Next
            ReadMessageThread.Clear()
        End If
    End Sub

    Private Sub SendMessageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SendMessageToolStripMenuItem.Click
        Dim NewMessageDialog As New sendmessage

        If NewMessageDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Disable Recieving
            Timer1.Enabled = False
            'Send Message
            For Each item In NewMessageDialog.ListofRecipient
                'Progress Show
                'MsgBox("ano")
                myengine.SendMessage(item, NewMessageDialog.Message)

            Next
        End If
        'Enable Recieving
        Timer1.Enabled = True
    End Sub
End Class
'Private Sub serialport1_datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'    'Pause while all data is read
'    Dim datain As String = ""
'    Dim numbytes As Integer = SerialPort1.BytesToRead

'    For i As Integer = 1 To numbytes
'        datain &= Chr(SerialPort1.ReadChar)
'    Next
'    'test(datain)

'End Sub


'Private Sub LoadMessages()


'End Sub
'Sub ReadAll()

'End Sub
'Sub ReadUnread()

'End Sub
'Sub MarkasRead()
'    'Send command
'    'Update the table
'End Sub




'Function Duplicate(stringKey As String) As Boolean
'    'Try
'    '    For Each item As ListViewItem In ListView1.Items
'    '        Dim myKey As String = item.SubItems(3).Text & item.SubItems(4).Text
'    '        If myKey = stringKey Then
'    '            Return True
'    '        End If
'    '    Next
'    'Catch ex As Exception
'    '    'MsgBox(ex.Message)
'    '    Return False
'    'End Try
'    'Return False
'End Function


'Private Sub btnLoadSMS_Click(sender As Object, e As EventArgs)
'    'LoadMessages()
'End Sub
'Dim index As String = ""
'Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs)
'    'Try
'    '    RichTextBox1.Text = ListView1.FocusedItem.SubItems(5).Text
'    '    With ListView1.FocusedItem
'    '        index = .SubItems(1).Text
'    '    End With
'    'Catch ex As Exception

'    'End Try
'End Sub

'Private Sub Button1_Click_1(sender As Object, e As EventArgs)
'    'Timer1.Enabled = True
'    'Dim t As New Threading.Thread(AddressOf OpenPort)
'    't.Start()
'End Sub

'Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'    'Dim t As New Threading.Thread(AddressOf myengine.InsertAllMessages)
'    't.Start()
'End Sub